window.Result;
var flg = true;
var Validform = {
    vali: function(params) {
        this.ele = params.ele;
        this.items = params.items;
        this._self = params._self;
        this.beforeCheck = params.beforeCheck;
        this.beforeSubmit = params.beforeSubmit;
        this.fromID = params.fromID;
        this.datatype(this.items, this._self);
        
    },
    GetLength: function(str, type) {
        if (type) {
            type = type
        } else {
            type = 0
        }
        var realLength = 0,
            len = str.length,
            charCode = -1;
        for (var i = 0; i < len; i++) {
            charCode = str.charCodeAt(i);
            if (type == 1) {
                if (charCode >= 0 && charCode <= 128)
                    realLength += 0.5;
                else {
                    realLength += 1;
                }
            } else {
                if (charCode >= 0 && charCode <= 128)
                    realLength += 1;
                else {
                    realLength += 1;
                }
            }
        }
        return Math.round(realLength);
    },
    datatype: function(ops, _self) {
        var _this = this;
        var submitBtn = document.querySelectorAll(this.ele)[0];
        var data = [];
        submitBtn.onclick = function() {
            Result = true;
            for (let i = 0; i < ops.length; i++) {
                var o = document.querySelectorAll(".Rcheck .required")[i];
                data = ops[i]
                _this.checkAll(data, o, _self, i)
            }
            if (Result && flg) {
                //校验前
                _this.beforeCheck()
                if(_this.fromID){
                   _this.beforeSubmit(_this.customFun.formser(_this.fromID))
                }
                else
                {
                   _this.beforeSubmit(_this.customFun.formser("postForm")) 
                }
            }
            return Result && flg
        }
    },
    Result: function(flag) {
        return flag
    },
    onError: function(o, _self, i, type, msg) {
        var flag = _self.state.init.flag;
        var val = _self.state.init.val;
        if (type == 0) {
            _self.setState({
                index: i,
                init: {
                    "msg": msg,
                    "flag": false,
                    "val": true
                }
            })
        } else {
            _self.setState({
                index: i,
                init: {
                    "msg": msg,
                    "flag": false,
                    "val": false
                }
            })
        }
        o.focus()
        Result = false
    },
    onSuccess: function(o, _self, i) {
        var flag = _self.state.init.flag;
        var val = _self.state.init.val;
        if (Result && flg) {
            _self.setState({
                index: i,
                init: {
                    "flag": true,
                    "val": false
                }
            })

        }
        Result = true

    },
    customFun: {
        run: (_self, i) => {
            console.log()

            if (Validform.items[i].reg) {
                eval("_self." + Validform.items[i].reg)
            }
        },
        range: function(T, data, callback) {
            if(typeof(callback) == "function") {
                flg = callback();
            }
        },
        formser: function(form) {
            var form = document.getElementById(form);
            var arr = {};
            for (var i = 0; i < form.elements.length; i++) {
                var feled = form.elements[i];
                switch (feled.type) {
                case undefined:
                case 'button':
                case 'file':
                case 'reset':
                case 'submit':
                    break;
                case 'checkbox':
                case 'radio':
                    if (!feled.checked) {
                        break;
                    }
                default:
                    if (arr[feled.name]) {
                        arr[feled.name] = arr[feled.name] + ',' + feled.value;
                    } else {
                        arr[feled.name] = feled.value;

                    }
                }
            }
            return arr
        }
    },
    checkAll: function(ops, o, _self, i) {
        var _this = this;
        var error_text = ops.error;
        var null_text = ops.null;
        var type_text = ops.datatype;
        var input_type = o.getAttribute("type");
        var tagName = o.tagName;

        var type = {
            "match": /^(.+?)(\d+)-(\d+)$/,
            "z": /[\w\W]+/,
            "*6-16": /^[\w\W]{6,16}$/,
            "n": /^$|^\d+$/,
            "n6-16": /^\d{6,16}$/,
            "s": /^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]+$/,
            "s6-18": /^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$/,
            "p": /^[0-9]{6}$/,
            "m": /^[1][3456789][0-9]{9}$/,
            "tel": /((^(0\d{2})-(\d{8})$)|(^(0\d{3})-(\d{7,8})$)|(^(0\d{2})-(\d{8})-(\d+)$)|(^(0\d{3})-(\d{7,8})-(\d+)$)|(^(0\d{2})(\d{8})$)|(^(0\d{3})(\d{7,8})$)|(^(0\d{2})(\d{8})-(\d+)$)|(^(0\d{3})(\d{7,8})-(\d+)$))|^[1][3456789][0-9]{9}$/,
            "e": /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            "xz": /(^[1-9]\d{0,1}$)|(^[1-9]\d{1}[0]$)/,
            "id": /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/,
            "url": /(http:\/\/|ftp:\/\/|https:\/\/|www)?[^\u4e00-\u9fa5\s\uFE30-\uFFA0]*?\.([a-zA-Z0-9]|[1-255])[^\u4e00-\u9fa5\s\uFE30-\uFFA0]*/
        }
        if (!flg) {
            return
        }
        if (Result) {
            if (o.value == "" || o.value == "请选择") {
                if (o.getAttribute("placeholder") != "选填") {
                    _this.onError(o, _self, i, 0, null_text)
                    Result = false
                }

            } else {
                if (typeof (type_text) != "undefined" && type_text.indexOf("/") == 0) {
                    if (o.value == "") {
                        _this.onError(o, _self, i, 0, null_text)
                        Result = false
                    } else {
                        if (!eval(type_text).test(o.value)) {
                            _this.onError(o, _self, i, 1, error_text)
                            Result = false
                        } else {
                            _this.onSuccess(o, _self, i);
                            _this.customFun.run(_self, i);

                        }
                    }
                } else {
                    tagName = tagName.toLowerCase()
                    if (tagName == "input") {
                        switch (input_type) {
                        case "text":
                            if (!eval("type." + type_text + ".test(o.value)")) {
                                _this.onError(o, _self, i, 1, error_text)
                                Result = false
                            } else {
                            
                                _this.onSuccess(o, _self, i);
                                _this.customFun.run(_self, i);
                            }
                            break;
                        case "radio":
                            var val;
                            var obj = document.getElementsByName(o.name);
                            for (var z = 0; z < obj.length; z++) {
                                if (obj[z].checked) {
                                    val = typeof (obj[z].value)
                                }
                            }
                            if (val == undefined) {
                                _this.onError(o, _self, i, 0, null_text)
                                Result = false
                            } else {
                                _this.onSuccess(o, _self, i)
                                _this.customFun.run(_self, i);
                            }
                            break;
                        case "checkbox":
                            var val;
                            var obj = document.getElementsByName(o.name);
                            for (var z = 0; z < obj.length; z++) {
                                if (obj[z].checked) {
                                    val = typeof (obj[z].value)
                                }
                            }
                            if (val == undefined) {
                                _this.onError(o, _self, i, 0, null_text)
                                Result = false
                            } else {
                                _this.onSuccess(o, _self, i)
                                _this.customFun.run(_self, i);
                            }
                            break;
                        default:
                            if (!eval("type." + type_text + ".test(o.value)")) {
                                _this.onError(o, _self, i, 1, error_text)
                                Result = false
                            } else {
                                _this.onSuccess(o, _self, i);
                                _this.customFun.run(_self, i);
                            }
                        }
                        if (tagName == "textarea") {
                            if (!eval("type." + type_text + ".test(o.value)")) {
                                _this.onError(o, _self, i, 1, error_text)
                                Result = false
                            } else {
                                _this.onSuccess(o, _self, i);
                                _this.customFun.run(_self, i);
                            }
                        }
                        if (tagName == "select") {
                            if (!eval("type." + type_text + ".test(o.value)")) {
                                _this.onError(o, _self, i, 1, error_text)
                                Result = false
                            } else {
                                _this.onSuccess(o, _self, i);
                                _this.customFun.run(_self, i);
                            }
                        }
                    }
                }
                return Result
            }
        }
        return Result
    }
};

export default Validform;